/**
 * Master Controller
 */

angular.module('RDash')
    .controller('MasterCtrl', ['app', 'restService', 'minaService', 'commonService', '$scope', '$cookieStore', '$location', '$window', '$http', MasterCtrl]);

function MasterCtrl(app, restService, minaService, commonService, $scope, $cookieStore, $location, $window, $http) {
    /**
     * Sidebar Toggle & Cookie Control
     */
    var mobileView = 992;

    $scope.getWidth = function () {
        return window.innerWidth;
    };

    $scope.$watch($scope.getWidth, function (newValue, oldValue) {
        if (newValue >= mobileView) {
            if (angular.isDefined($cookieStore.get('toggle'))) {
                $scope.toggle = !!$cookieStore.get('toggle');
            } else {
                $scope.toggle = true;
            }
        } else {
            $scope.toggle = false;
        }
    });

    $scope.toggleSidebar = function () {
        $scope.toggle = !$scope.toggle;
        $cookieStore.put('toggle', $scope.toggle);
    };

    window.onresize = function () {
        $scope.$apply();
    };

    /**
     * 自定义实现
     */
    console.log('master-ctrl');
    console.log('root : ' + restService.root);

    $scope.author = app.getAuthor();
    $scope.online = app.getOnline();
    $scope.routes = app.routes;

    // 如果已经有登录
    if ($scope.online) {
        // 如果已经得到授权的视图
        if ($scope.author.views != null) {
            console.log("views from remote !");

            var views = $scope.author.views;
            for (var i = 0; i < views.length; i++) {
                $scope.routes.push({
                    name: views[i]['description'],
                    path: views[i]['name'],
                    icon: views[i]['extraData']
                });
            }
        }
        // 如果没有设置昵称,则给他一个昵称
        if ($scope.author.nickname == null || $scope.author.nickname.length == 0) {
            $scope.author.nickname = '暂无';
        }
        // 连接WebSocket
        if ($scope.online) {
            // minaService.connect();
        }
    }

    // 做一次心跳探测,确认一下登录信息
    restService.api.index.save({ID: 'heart'}, null, function (result) {
        if (app.catchResultCode(result['resultCode']) && !$scope.online) {
            restService.api.user.save({
                ID: 'watchUserInfo'
            }, null, function (result) {
                if (app.catchResultCode(result['resultCode'])) {
                    var author = angular.fromJson(result['data']);
                    app.setAuthor(author);
                    $window.location.reload();
                }
            });
        }
    });

    commonService.toast.showOToasts();

    $scope.onClickMine = function () {
        $location.path('/auth');
    };

    // 注销
    $scope.logout = function () {
        if (!$scope.online) {
            return;
        }

        $http({
            method: 'POST',
            url: restService.create('index/logout')
        }).then(function successCallback(response) {
            if (response.data['resultCode'] == 0) {
                app.catchResultCode(0x09)
            }
        });
    };

    $scope.setIncludePath = function (include_path) {
        app.setIncludePath(include_path);

        $scope.$broadcast('include_path', include_path);
    };
}